﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Animaonline.WeatherAPI;
using System.Speech.Synthesis;
using System.Net.Mail;

namespace JarvisV2
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        PromptBuilder pbuilder = new PromptBuilder();
        PromptStyle pStyle = new PromptStyle();
        
        
        private WeatherData wD = new WeatherData();
        [DllImport("user32.dll")]
        public static extern void LockWorkStation();
        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

        string[,] vcArray = new string[1, 4];
        string voiceComPath = @"C:\Justin's Stuff\VS2010 Projects\JarvisV2\JarvisV2\Properties\voice commands.txt";
        SpeechRecognizer speechReco = new SpeechRecognizer();
        List<string> grammerList = new List<string>();

        public Form1()
        {
            InitializeComponent();
            this.grammerList.Add("one");
            this.grammerList.Add("two");
            Choices myChoices = new Choices(grammerList.ToArray());
            
            

        }

        void speechReco_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            String speech = e.Result.Text;
            
            
            for (int row = 0; row < (vcArray.Length / 4) - 1; row++)
            {
                if (speech == vcArray[row, 1])
                {
                    txtReco.Text = vcArray[row, 0];
                    Process.Start(vcArray[row, 2], vcArray[row, 3]);
                }
            }
            switch (speech)
            {

                case "Volume Up":
                    Volume("up");
                    break;
                case "Volume Down":
                    Volume("down");
                    break;
                case "Jarvis Lock Computer":
                    LockWorkStation();
                    break;
                //case "Jarvis Close this":
                //    SendKeys.Send("%{F4}");
                //    break;
                case "Jarvis get rid of this":
                    SendKeys.Send("%{F4}");
                    break;

                case "Escape":
                    SendKeys.Send("{ESC}");
                    break;
                case "Jarvis how cold is it":
                    PromptBuilder pbuilder = new PromptBuilder();
                    
                    pbuilder.StartStyle(pStyle);
                    pbuilder.StartParagraph();
                    pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Teen, 2);
                    pbuilder.StartSentence();
                    pbuilder.AppendText("Checking");
                    pbuilder.EndSentence();
                    pbuilder.EndVoice();
                    pbuilder.EndParagraph();
                    pbuilder.EndStyle();
                    ss.SpeakAsync(pbuilder);
                    pbuilder = new PromptBuilder();
                    wD = WeatherAPI.GetWeather(LanguageCode.en_US, "Watauga, TX");
                    pbuilder.StartStyle(pStyle);
                    pbuilder.StartParagraph();
                    pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Teen, 2);
                    pbuilder.StartSentence();
                    pbuilder.AppendText("It is " + wD.temp_f.ToString() + " degrees outside.  ");
                    if (wD.temp_f < 45)
                        pbuilder.AppendText("Put_on a jacket");
                    pbuilder.EndSentence();
                    pbuilder.EndVoice();
                    pbuilder.EndParagraph();
                    pbuilder.EndStyle();
                    ss.SpeakAsync(pbuilder);
                    break;



            }
        }

        void Volume(string change)
        {
            uint CurrVol = 0;
            // At this point, CurrVol gets assigned the volume
            waveOutGetVolume(IntPtr.Zero, out CurrVol);
            // Calculate the volume
            ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
            // Get the volume on a scale of 1 to 10 (to fit the trackbar)
            int volume = CalcVol / (ushort.MaxValue / 10);
            MessageBox.Show(volume.ToString());
            if (volume < 10 && change == "up")
                volume++;
            if (volume > 0 && change == "down")
                volume--;
            int NewVolume = ((ushort.MaxValue / 10) * volume);
            // Set the same volume for both the left and the right channels
            uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
            // Set the volume
            waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
            
            
        }
        

        private void cmdEnable_Click(object sender, EventArgs e)
        {
            txtarray.Text = "Text\t\tCommand\t\t\tPath\t+ Args";
            vcArray = new string[1, 4];
            label4.Text = "Initializing Speech Engine....";
            StreamReader sr = new StreamReader(voiceComPath);

            vcArray[0, 0] = "";
            string line = "";

            int x = 0, y = 0, b = 0;
            do
            {
                do
                {
                    line = sr.ReadLine();
                    if (line != null)
                    {

                        if (line.Contains("EOF"))
                            break;
                        else if (!line.Contains("---"))
                        {
                            if (line.Length < 2)
                                b++;
                            if (b > 1)
                                break;
                            if (line != null)
                                vcArray[x, y] = line;
                            else
                                vcArray[x, y] = "";
                            y++;
                        }
                    }
                } while (y < 4 && !sr.EndOfStream);

                if (line.Contains("EOF"))
                    break;
                else
                {
                    ResizeArray(ref vcArray, 4, x + 2);
                    b = 0;
                    y = 0;
                    x++;

                }
            } while (true);
            sr.Close();

            label3.Text = "";
            for (int row = 0; row < (vcArray.Length / 4) - 1; row++)
            {
                txtarray.Text += "\n" + vcArray[row, 0] + "\t\t" + vcArray[row, 1] + "\t\t\t" + vcArray[row, 2] + "\t" + vcArray[row, 3];
                AddWord(vcArray[row, 1]);
                label3.Text += vcArray[row, 0] + "\n";
            }
            AddWord("Volume Up");
            AddWord("Volume Down");
            AddWord("Jarvis Lock Computer");
            AddWord("Jarvis Close this");
            AddWord("Jarvis get rid of this");
            AddWord("Escape");
            AddWord("Jarvis how cold is it"); 
            Choices myChoices = new Choices(grammerList.ToArray());
            GrammarBuilder builder = new GrammarBuilder(myChoices);

            Grammar gram = new Grammar(builder);

            speechReco.LoadGrammar(gram);
            gram.Enabled = true;

            speechReco.Enabled = true;
            

            speechReco.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(speechReco_SpeechRecognized);
            label4.Text = "Speech Engine Ready for Input";
        }

        private void ResizeArray(ref string[,] original, int cols, int rows)
        {
            //create a new 2 dimensional array with
            //the size we want
            string[,] newArray = new string[rows, cols];
            //copy the contents of the old array to the new one
            Array.Copy(original, newArray, original.Length);
            //set the original to the new array
            original = newArray;
        }

        private void AddWord(string word)
        {
            this.grammerList.Add(word);
        }

        private void cmdDiable_Click(object sender, EventArgs e)
        {
            speechReco.Enabled = false;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            
            txtarray.Text = wD.temp_f.ToString();
            PromptBuilder pbuilder = new PromptBuilder();

            pbuilder.StartStyle(pStyle);
            pbuilder.StartParagraph();
            pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Teen, 2);
            pbuilder.StartSentence();
            pbuilder.AppendText("Checking current weather.");
            pbuilder.EndSentence();
            pbuilder.EndVoice();
            pbuilder.EndParagraph();
            pbuilder.EndStyle();
            ss.SpeakAsync(pbuilder);
            
            //pbuilder = new PromptBuilder();
            //wD = WeatherAPI.GetWeather(LanguageCode.en_US, "Watauga, TX");
            //pbuilder.StartStyle(pStyle);
            //pbuilder.StartParagraph();
            //pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Teen, 2);
            //pbuilder.StartSentence();
            //pbuilder.AppendText("It is " + wD.temp_f.ToString() + " degrees outside.  ");
            //if (wD.temp_f < 45)
            //    pbuilder.AppendText("Puton a jacket");
            //pbuilder.EndSentence();
            //pbuilder.EndVoice();
            //pbuilder.EndParagraph();
            //pbuilder.EndStyle();
            //ss.SpeakAsync(pbuilder);
        }
    }
}
